package com.bolt.admin.web.controller;

import com.bolt.admin.common.base.SimpleController;
import com.bolt.admin.module.sys.service.OnlineUserService;
import com.bolt.admin.security.shiro.AuthContextHolder;
import com.bolt.admin.security.shiro.AuthUserDetails;
import com.bolt.admin.security.shiro.session.OnlineSession;
import com.bolt.admin.security.shiro.session.OnlineSessionDAO;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Returns;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2020/10/21.
 */
@Controller
@RequestMapping("/admin/sys/online")
public class OnlineController extends SimpleController {

    @Autowired
    private OnlineUserService onlineUserService;

    @Autowired
    private OnlineSessionDAO onlineSessionDAO;

    /**
     * 模块首页
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView index(Model model, HttpServletRequest request) {
        return super.index("/admin/sys/online/index", request, model.asMap());
    }

    @RequestMapping(value = "page")
    @ResponseBody
    public ResultMessage getOrgList(Pageable pageable, String userName) {
        return Returns.success(onlineUserService.getAll(userName, pageable));
    }

    /**
     * 踢出用户
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "kickOut")
    @ResponseBody
    public ResultMessage kickOut(String key) {
        AuthUserDetails authUserDetails = AuthContextHolder.getAuthUserDetails();
        if (key.equals(authUserDetails.getKey())) {
            return Returns.warning("当前登录用户无法强退");
        }
        OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(key);
        if (onlineSession == null) {
            return Returns.warning("用户已下线");
        }
        onlineSessionDAO.delete(onlineSession);
        onlineUserService.logOut(key);
        return Returns.success();
    }


}

 